﻿//
// ApplicationSettingsControl.cs: Manages application settings.
//
// Copyright 2010 Michael Goethe
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;
using S9PalCommon;

namespace S9Pal
{
    /// <summary>
    /// Manages application settings.
    /// </summary>
    public partial class ApplicationSettingsControl : UserControl, ISettingsControl
    {
        #region Fields

        private String displayTitle = "General";

        #endregion

        #region Constructors

        public ApplicationSettingsControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Text to display for the SettingsControl.
        /// </summary>
        public String DisplayTitle
        {
            get
            {
                return displayTitle;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Update controls by pulling applicable values from the settings object.
        /// </summary>
        public void UpdateControlValues()
        {
            pullLibraryFromDeviceCheckBox.Checked = Global.Settings.PullLibraryFromDevice;
            if (Global.Settings.CustomLibraryDirectory != null)
                customLibraryPathTextBox.Text = Global.Settings.CustomLibraryDirectory.ToString();
        }

        /// <summary>
        /// Apply current values to the settings object.
        /// </summary>
        public void ApplySettings()
        {
            Global.Settings.PullLibraryFromDevice = pullLibraryFromDeviceCheckBox.Checked;

            try
            {
                Global.Settings.CustomLibraryDirectory = new DirectoryInfo(customLibraryPathTextBox.Text);
            }
            catch (ArgumentException ex)
            {
                if (Global.Settings.PullLibraryFromDevice)
                    Global.Settings.CustomLibraryDirectory = null;
                else
                    throw (new ArgumentException("Invalid custom library directory. Please enter a valid directory.", ex));
            }
        }

        #endregion

        #region Events

        private void browseButton_Click(object sender, EventArgs e)
        {
            // Browse for a custom library path.
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();

            customLibraryPathTextBox.Text = fbd.SelectedPath;
        }

        private void pullLibraryFromDeviceCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            // Update the other controls to reflect the state of pullLibraryFromDevice.
            bool isEnabled = !pullLibraryFromDeviceCheckBox.Checked;
            customLibraryPathLabel.Enabled = isEnabled;
            customLibraryPathTextBox.Enabled = isEnabled;
            browseButton.Enabled = isEnabled;
        }

        #endregion
    }
}
